/**
 * 
 */
package impl;

import interfaces.ICandidate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import utils.MailTemplate;

import com.atlassian.jira.rpc.soap.beans.RemoteCustomFieldValue;
import com.atlassian.jira.rpc.soap.beans.RemoteIssue;

/**
 *
 * @author jbrasca
 * date: Aug 5, 2011
 */
public class Candidate implements ICandidate{
	
	private MailTemplate mailTemplate;
	private RemoteIssue remoteIssue;
	private String mailCustomFieldId;
	private String sourceCustomFieldId;
	private String office;
	private String name;
	private String email;
	private String phoneNumber;

	public Candidate(RemoteIssue remoteIssue){
		this.mailCustomFieldId = "customfield_10000";
		this.sourceCustomFieldId = "customfield_10010";
		this.remoteIssue =  remoteIssue;
		getCandidateData(getRemoteIssue().getSummary(),getRemoteIssue().getDescription());
		this.mailTemplate =  new MailTemplate(getOffice(), getName());
	}
	
	private void getCandidateData(String summary, String description){
		
		//Finds the candidate office
// 		Integer beginIndex = description.indexOf("Application for: ");
//		Integer endIndex = description.indexOf("\n", beginIndex);
//		String applicationFor = description.substring("Application for: ".length() + beginIndex, endIndex);
		
		
		String[] splits = summary.split("\\|");
		if(splits.length == 2){
			setName(splits[1]);
			String jobTitle = splits[0];
			String office =  findOfficeByJobTitle(jobTitle);
			if(office != null){
				setOffice(office);
			}
		}
		
		
		String email = new String();
		
		//Finds the candidate email
		Integer beginIndex = description.indexOf("Contact Information");
		String aux = description.substring(beginIndex);
		//Set the email pattern string
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		for(String line:aux.split("\n")){
			//Match the given string with the pattern
		     Matcher m = p.matcher(line);
		     if(m.matches()){
		    	 email=m.group();
		    	 setEmail(email);
		    	 return;
		     }
		}
		
	}
	
	/**
	 * @param jobTitle
	 * @return
	 */
	private String findOfficeByJobTitle(String jobTitle) {
		String office = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/test","root", "root");
			Statement s = connection.createStatement();
			ResultSet rs = s.executeQuery ("select property from properties where propertyValue = \""+jobTitle.trim()+"\"" );
			if(rs.next()){
				String property =rs.getString(1);
				office = property.replaceAll("JobTitle","");
			}
			connection.close();
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}		
		
		return office;
	}

	public String getEmail() {
		
		if(this.email == null){
			for(RemoteCustomFieldValue customFieldValue : getRemoteIssue().getCustomFieldValues()){
				if(customFieldValue.getCustomfieldId().equals(getMailCustomFieldId())){
					return customFieldValue.getValues()[0];
				}
			}
		}
		return email;
	}
	
	public String getOffice(){
		if(office == null){
			for(RemoteCustomFieldValue customFieldValue : getRemoteIssue().getCustomFieldValues()){
				if(customFieldValue.getCustomfieldId().equals(getSourceCustomFieldId())){
					office =  customFieldValue.getValues()[0];
				}
			}
		}
		
		return office;
	}
	

	public void setPhoneNumeber(String phoneNumeber) {
		this.phoneNumber = phoneNumeber;
	}
	
	public void setOffice(String office) {
		this.office = office;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}


	private String getMailCustomFieldId() {
		return mailCustomFieldId;
	}

	private String getSourceCustomFieldId() {
		return sourceCustomFieldId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private String getPhoneNumber() {
		return phoneNumber;
	}

	
	public String getEmailSubject(){
		return getMailTemplate().getMailSubject();
	}
	
	public String getEmailContent(){
		return getMailTemplate().getMailContent();
	}

	private MailTemplate getMailTemplate() {
		return mailTemplate;
	}

	private RemoteIssue getRemoteIssue() {
		return remoteIssue;
	}
	
	
	
}
